import { loginByEmail, logout, getInfo } from 'api/login';
import Cookies from 'js-cookie';
// import { start } from 'repl';
import sha256 from 'sha256';

const user = {
    state: {
        user: '',
        status: '',
        code: '',
        uid: Cookies.get('uid'),
        roleId: Cookies.get('roleId'),
        roleType: Cookies.get('roleType'),
        agentTypes: Cookies.get('agentTypes'),
        account: Cookies.get('account'),
        password: Cookies.get('password'),
        auserName: Cookies.get('auserName'),
        authType: '',
        token: Cookies.get('accessToken'),
        name: '',
        avatar: '',
        introduction: '',
        roles: [],
        menuGenerated: false,
        setting: {
            articlePlatform: []
        },
        // clientType: Cookies.get('clientType'), // 为了统计在线用户的信息 by cuihd 20171229
        operateType: Cookies.get('operateType')
    },

    mutations: {
        SET_AUTH_TYPE: (state, type) => {
            state.authType = type;
        },
        SET_CODE: (state, code) => {
            state.code = code;
        },
        SET_TOKEN: (state, token) => {
            state.token = token;
        },
        SET_UID: (state, uid) => {
            state.uid = uid;
        },
        SET_ROLEID: (state, roleId) => {
            state.roleId = roleId;
        },
        SET_ROLETYPE: (state, roleType) => {
            state.roleType = roleType
        },
        SET_AGENTTYPES: (state, agentTypes) => {
            state.agentTypes = agentTypes
        },
        SET_MENU_GENERATED: (state, menuGenerated) => {
            state.menuGenerated = menuGenerated;
        },
        SET_EMAIL: (state, email) => {
            state.email = email;
        },
        SET_INTRODUCTION: (state, introduction) => {
            state.introduction = introduction;
        },
        SET_SETTING: (state, setting) => {
            state.setting = setting;
        },
        SET_STATUS: (state, status) => {
            state.status = status;
        },
        SET_NAME: (state, name) => {
            state.name = name;
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar;
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles;
        },
        LOGIN_SUCCESS: () => {
            console.log('login success')
        },
        LOGOUT_USER: state => {
            state.user = '';
        },
        SET_USERNAME: (state, account) => {
            state.account = account
        },
        SET_PASSWORD: (state, password) => {
            state.password = password;
        },
        SET_AUSERNAME: (state, userName) => {
            state.auserName = userName;
        }
        // SET_CLIENTTYPE: (state, clientType) => { // 为了统计在线用户的信息 by cuihd 20171229
        //     state.clientType = clientType;
        // }
    },

    actions: {
        LoginByUsername({ commit }, userInfo) {
            const account = userInfo.account.trim();
            const password = userInfo.password.trim();
            const isRemberAccount = userInfo.rUname;
            const isRemberPassword = userInfo.rPass;
            const pwd = sha256(password);
            return new Promise((resolve, reject) => {
                loginByEmail(account, pwd).then(response => {
                    const data = response.data.content;
                    Cookies.set('uid', data.userId);
                    Cookies.set('accessToken', data.token);
                    Cookies.set('roleId', data.roleId);
                    Cookies.set('auserName', data.userName);
                    Cookies.set('roleType', data.roleType);
                    Cookies.set('agentTypes', JSON.stringify(data.agentTypes));
                    // Cookies.set('clientType', '1:1'); // 为了统计在线用户的信息(后期修改) by cuihd 20171229
                    // commit('SET_CLIENTTYPE', '1:1'); // 为了统计在线用户的信息(后期修改) by cuihd 20171229
                    commit('SET_AUSERNAME', data.userName);
                    commit('SET_ROLEID', data.roleId);
                    commit('SET_UID', data.userId);
                    commit('SET_TOKEN', data.token);
                    commit('SET_ROLETYPE', data.roleType);
                    commit('SET_AGENTTYPES', JSON.stringify(data.agentTypes));
                    if (isRemberAccount) {
                        Cookies.set('account', account, 30);
                        commit('SET_USERNAME', account);
                    } else {
                        Cookies.remove('account');
                        commit('SET_USERNAME', '');
                    }
                    if (isRemberPassword) {
                        Cookies.set('password', password, 30);
                        commit('SET_PASSWORD', password);
                    } else {
                        Cookies.remove('password');
                        commit('SET_PASSWORD', '');
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },

        // 获取用户信息
        GetInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                getInfo(state.roleId).then(response => {
                    // const data = response.data.content;
                    // commit('SET_ROLES', data.role);
                    // commit('SET_NAME', data.upmsUser.realname);
                    // commit('SET_AVATAR', data.upmsUser.avatar);
                    // commit('SET_UID', data.uid);
                    // commit('SET_INTRODUCTION', data.introduction);
                    commit('SET_MENU_GENERATED', true);
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },
        // GetInfo({ commit, state }) {
        //   return new Promise((resolve, reject) => {
        //     getInfo(state.token,state.uid).then(response => {
        //       const data = response.data;
        //       commit('SET_ROLES', data.role);
        //       commit('SET_NAME', data.name);
        //       commit('SET_AVATAR', data.avatar);
        //       commit('SET_UID', data.uid);
        //       commit('SET_INTRODUCTION', data.introduction);
        //       resolve(response);
        //     }).catch(error => {
        //       reject(error);
        //     });
        //   });
        // },


        // 登出
        LogOut({ commit, state }) {
            return new Promise((resolve, reject) => {
                console.log('logout', logout);
                // logout(state.token).then(() => {
                commit('SET_UID', '');
                commit('SET_TOKEN', '');
                commit('SET_ROLES', []);
                commit('SET_AUSERNAME', '');
                // commit('SET_CLIENTTYPE', ''); // 为了统计在线用户的信息 by cuihd 20171229
                // Cookies.remove('clientType'); // 为了统计在线用户的信息 by cuihd 20171229
                Cookies.remove('uid');
                Cookies.remove('accessToken');
                Cookies.remove('auserName');
                resolve();
                // }).catch(error => {
                //     reject(error);
                // });
            });
        },

        // 前端 登出
        FedLogOut({ commit }) {
            return new Promise(resolve => {
                commit('SET_UID', '');
                commit('SET_TOKEN', '');
                Cookies.remove('uid');
                Cookies.remove('accessToken');
                resolve();
            });
        },

        // 动态修改权限
        ChangeRole({ commit }, role) {
            return new Promise(resolve => {
                commit('SET_ROLES', [role]);
                commit('SET_TOKEN', role);
                Cookies.set('accessToken', role);
                resolve();
            })
        }
    }
};

export default user;
